package com.ruoyi.sale.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.BaseLottery;
import com.ruoyi.base.domain.vo.LotteryItemVo;
import com.ruoyi.base.domain.vo.LotteryVo;
import com.ruoyi.base.service.IBaseLotteryService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sale.domain.SaleLotteryOne;
import com.ruoyi.sale.domain.SaleLotterySix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.sale.mapper.SaleLotteryThreeMapper;
import com.ruoyi.sale.domain.SaleLotteryThree;
import com.ruoyi.sale.service.ISaleLotteryThreeService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 组三彩票售卖Service业务层处理
 *
 * @author szm
 * @date 2025-09-25
 */
@Service
public class SaleLotteryThreeServiceImpl extends ServiceImpl<SaleLotteryThreeMapper,SaleLotteryThree> implements ISaleLotteryThreeService {

    @Autowired
    private IBaseLotteryService baseLotteryService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<LotteryVo> buyNumLottery(LotteryVo lotteryVo) {
        List<LotteryVo> voList = new ArrayList<>();
        if (lotteryVo != null){
            List<BaseLottery> updateList = new ArrayList<>();
            List<SaleLotteryThree> entityList = new ArrayList<>();
            String baseId = IdUtils.snowflakeId();
            Map<String, Set<String>> setMap = lotteryVo.getSetMap();
            for (String key : setMap.keySet()){
                Set<String> set = setMap.get(key);
                String keyph = set.stream().collect(Collectors.joining(","));
                for (String str : set){
                    SaleLotteryThree entity = new SaleLotteryThree();
                    entity.setLotteryNo(str);
                    entity.setLotteryDate(new Date());
                    entity.setMultiple(lotteryVo.getMultiple());
                    entity.setUserName(lotteryVo.getBuyName());
                    entity.setUserPhone(lotteryVo.getBuyPhone());
                    entity.setWeixinName(lotteryVo.getBuyWeiName());
                    entity.setWeixinNo(lotteryVo.getBuyweiNo());
                    entity.setLotteryType(2);
                    entity.setLotteryName("组三");
                    entity.setMultiple(lotteryVo.getMultiple());
                    entity.setBaseBuyNo(key);
                    entity.setRemark(keyph);
                    entity.setBaseId(baseId);
                    entity.setCreateTime(new Date());
                    entity.setUpdateTime(new Date());
                    entity.setCreateBy(SecurityUtils.getUsername());
                    entity.setUpdateBy(SecurityUtils.getUsername());
                    entityList.add(entity);

                    BaseLottery updateVo = baseLotteryService.isMaxBuyNo(lotteryVo.getMultiple(),str,2);
                    if (updateVo.getStatus() == 2){
                        throw new ArithmeticException("组三，彩票号：【"+key+"】购买次数大于最大设置次数，请重新输入!");
                    } else {
                        updateList.add(updateVo);
                    }

                }

                LotteryVo vo = new LotteryVo();
                vo.setRefNo(key);
                vo.setRemark(keyph);
                vo.setLotteryType(2);

                voList.add(vo);
            }
            baseLotteryService.updateBatchById(updateList);
            this.saveBatch(entityList);
        }
        return voList;
    }

    @Override
    public List<LotteryVo> buyOneNumLottery(Map<String, Long> longMap,LotteryVo lotteryVo) {
        List<LotteryVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(longMap)){
            List<BaseLottery> updateList = new ArrayList<>();
            List<SaleLotteryThree> entityList = new ArrayList<>();
            String baseId = IdUtils.snowflakeId();

            Set<String> set = longMap.keySet();
            String keyph = set.stream().collect(Collectors.joining(","));

            for (String key : longMap.keySet()){
                int i = longMap.get(key).intValue();
                SaleLotteryThree entity = new SaleLotteryThree();
                entity.setLotteryNo(key);
                entity.setLotteryDate(new Date());

                BigDecimal multiple = lotteryVo.getBuyGroup().multiply(new BigDecimal(i));
                entity.setMultiple(multiple);

                entity.setUserName(lotteryVo.getBuyName());
                entity.setUserPhone(lotteryVo.getBuyPhone());
                entity.setWeixinName(lotteryVo.getBuyWeiName());
                entity.setWeixinNo(lotteryVo.getBuyweiNo());
                entity.setLotteryType(2);
                entity.setLotteryName("组三");

                entity.setRemark(lotteryVo.getLotteryNo());
                entity.setBaseBuyNo(key);
                entity.setBaseId(baseId);
                entity.setCreateTime(new Date());
                entity.setUpdateTime(new Date());
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setUpdateBy(SecurityUtils.getUsername());
                entityList.add(entity);

                BaseLottery updateVo = baseLotteryService.isMaxBuyNo(multiple,key,2);
                if (updateVo.getStatus() == 2){
                    baseLotteryService.delRedis();
                    throw new ArithmeticException("组三，彩票号：【"+key+"】购买次数大于最大设置次数，请重新输入!");
                } else {
                    updateList.add(updateVo);
                }
                LotteryVo vo = new LotteryVo();
                vo.setRefNo(key);
                vo.setRemark(keyph);
                vo.setLotteryType(2);

                voList.add(vo);
            }

            //baseLotteryService.updateBatchById(updateList);
            //this.saveBatch(entityList);
            redisCache.setCacheList("threeUpBuy",updateList);
            redisCache.setCacheList("threeData",entityList);
        }
        return voList;
    }

    @Override
    public List<LotteryVo> getImageList(String baseId) {
        List<SaleLotteryThree> imageList = baseMapper.getImageList(baseId);
        List<LotteryVo> voList = imageList.stream().map(x -> {
            LotteryVo one = new LotteryVo();
            one.setRemark(x.getRemark());
            one.setRefNo(x.getBaseBuyNo());
            return one;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<LotteryVo> buyNum3DSingle(LotteryVo lotteryVo) {
        List<LotteryVo> voList = new ArrayList<>();
        List<BaseLottery> updateList = new ArrayList<>();
        List<SaleLotteryThree> entityList = new ArrayList<>();
        List<LotteryItemVo> itemList = lotteryVo.getItemList();
        String baseId = IdUtils.snowflakeId();
        for (LotteryItemVo item : itemList){
            String sort = item.getItemVal();
            item.setItemVal(sort);

            SaleLotteryThree entity = new SaleLotteryThree();
            entity.setLotteryNo(sort);
            entity.setLotteryDate(new Date());
            entity.setUserName(lotteryVo.getBuyName());
            entity.setUserPhone(lotteryVo.getBuyPhone());
            entity.setWeixinName(lotteryVo.getBuyWeiName());
            entity.setWeixinNo(lotteryVo.getBuyweiNo());
            entity.setLotteryType(2);
            entity.setLotteryName("组三 3D单式");
            entity.setBaseBuyNo(sort);
            entity.setMultiple(item.getItemMult());
            entity.setRemark(sort);
            entity.setBaseId(baseId);
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setCreateBy(SecurityUtils.getUsername());
            entity.setUpdateBy(SecurityUtils.getUsername());
            entityList.add(entity);
            BaseLottery updateVo = baseLotteryService.isMaxBuyNo(item.getItemMult(), sort, 2);
            if (updateVo.getStatus() == 2){
                throw new ArithmeticException("直选，彩票号：【"+sort+"】购买次数大于最大设置次数，请重新输入!");
            } else {
                updateList.add(updateVo);
            }
            LotteryVo vo = new LotteryVo();
            vo.setRefNo(sort);
            vo.setRemark(sort);
            vo.setLotteryType(2);
            voList.add(vo);
        }

        redisCache.setCacheList("threeUpBuy",updateList);
        redisCache.setCacheList("threeData",entityList);

        return voList;
    }

    @Override
    public int deleteAll() {
        return baseMapper.deleteAll();
    }


}
